package com.esun.rabbit2.communicaters.recharg;

import java.io.IOException;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;



/** 
* @ClassName: ServerCommunicater 
* @Description: TODO(数据通讯类，该类只执行post工作) 
* @author Miko baoz@500wan.com
* @date 2011-1-13 下午09:34:10 
*  
*/
public class ServerCommunicater {
	
	
	/**
	 * <P>执行post操作</p>
	 * @param postData 需要向server post的数据
	 * @author Miko
	 * */
	public static byte[] doPost(byte[] postData){
		
		HttpResponse httpResponse = null;
		
		HttpParams httpParameters = new BasicHttpParams(); 
		
		HttpConnectionParams.setConnectionTimeout(httpParameters, 10000); 
		
//		HttpConnectionParams.setSoTimeout(httpParameters, 10000);
		
		DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters); 
		
		HttpPost httpRequest = new HttpPost(DataShare.POSTADDRESS);
		
		ByteArrayEntity arrayEntity = new ByteArrayEntity(postData);
		arrayEntity.setContentType("application/octet-stream");
		
		httpRequest.setEntity(arrayEntity);
		
		byte[] output = null;
		try {
			
			httpResponse =(HttpResponse)httpClient.execute(httpRequest);
			output = EntityUtils.toByteArray(httpResponse.getEntity());
			
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		return output;
	}
	
}
